package com.jfirer.validator.constraint.validator;

import java.math.BigDecimal;
import java.math.BigInteger;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.constraints.Min;

public class MinValidator implements ConstraintValidator<Min, Number>
{
    private long minValue;
    
    @Override
    public void initialize(Min minValue)
    {
        this.minValue = minValue.value();
    }
    
    @Override
    public boolean isValid(Number value, ConstraintValidatorContext constraintValidatorContext)
    {
        if (value == null)
        {
            return true;
        }
        else if (value instanceof Double)
        {
            if ((Double) value == Double.POSITIVE_INFINITY)
            {
                return true;
            }
            else if (Double.isNaN((Double) value) || (Double) value == Double.NEGATIVE_INFINITY)
            {
                return false;
            }
        }
        else if (value instanceof Float)
        {
            if ((Float) value == Float.POSITIVE_INFINITY)
            {
                return true;
            }
            else if (Float.isNaN((Float) value) || (Float) value == Float.NEGATIVE_INFINITY)
            {
                return false;
            }
        }
        
        if (value instanceof BigDecimal)
        {
            return ((BigDecimal) value).compareTo(BigDecimal.valueOf(minValue)) != -1;
        }
        else if (value instanceof BigInteger)
        {
            return ((BigInteger) value).compareTo(BigInteger.valueOf(minValue)) != -1;
        }
        else
        {
            long longValue = value.longValue();
            return longValue >= minValue;
        }
    }
}
